<?php

namespace App\Http\Controllers;

use App\Models\Address;
use App\Models\User;
use EasyWeChat\Factory;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class UserController extends Controller
{
    public function login(Request $request){
        $code = $request->input('code');
        Log::info('code:'.$code);
        $config = [
            'app_id'    => config('wxxcx.appid'),
            'secret'    => config('wxxcx.app_secret'),
        ];
        $app = Factory::miniProgram($config);

        $response = $app->auth->session($code);

        if (isset($response['errcode'])) {
            renderError($response['errmsg']);
        }

        if (isset($result['openid'])) {
            // 根据 openid 查找或创建用户
            $user = User::firstOrCreate(
                ['openid' => $result['openid']],
                ['name' => 'User-' . $result['openid']]
            );

            // 生成访问令牌
            $token = $user->createToken('miniProgram')->plainTextToken;
            return renderSuccess(['token' => $token]);
        }

        return renderError('登录失败');
    }

    public function info(){
        $user = auth()->user();
        return response()->json(['user' => $user]);
    }

    public function addressList(){
        $user = auth()->user();
        $address = $user->address()->select(['id','name','province','city','area','address','mobile','default'])->get();
        return renderSuccess($address);
    }

    // 添加收货地址
    public function addAddress(){
        $user = auth()->user();
        $data = request()->all();
        $data['user_id'] = $user->id;
        $data['created_at'] = date('Y-m-d H:i:s');
        $data['updated_at'] = date('Y-m-d H:i:s');
        // 判断是否设置默认地址
        if ($data['default'] == 1){
            $user->address()->update(['default' => 0]);
        }
        $address = $user->address()->insert($data);
        return renderSuccess($address);
    }

    // 删除收货地址
    public function removeAddress(){
        $user = auth()->user();
        $address_id = request('address_id');
        $address = $user->address()->find($address_id);
        if (!$address){
            return renderError('收货地址不存在');
        }
        $address->delete();
        return renderSuccess();
    }

    // 获取收货地址详情
    public function addressDetail(){
        $user = auth()->user();
        $address_id = request('address_id');
        $address = $user->address()->select(['id','name','province','city','area','address','mobile','default'])->find($address_id);
        if (!$address){
            return renderError('收货地址不存在');
        }
        return renderSuccess($address);
    }

    // 修改收货地址
    public function updateAddress(){
        $user = auth()->user();
        $address_id = request('address_id');
        $address = $user->address()->find($address_id);
        if (!$address){
            return renderError('收货地址不存在');
        }
        $data = request()->all();
        unset($data['address_id']);
        $user->address()->where('id',$address_id)->update($data);
        return renderSuccess($address);
    }

    // 设置默认收货地址
    public function setDefaultAddress(){
        $user = auth()->user();
        $address_id = request('address_id');
        $address = $user->address()->find($address_id);
        if (!$address){
            return renderError('收货地址不存在');
        }
        $user->address()->update(['default' => 0]);
        $user->address()->where('id',$address_id)->update(['default' => 1]);
        return renderSuccess();
    }
}
